package com.gj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gj.mapper.TransactionMessageMapper;
import com.gj.model.TransactionMessage;
import com.gj.service.TransactionMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * author:guijie
 * date:${date}
 * description:TODO
 */
@Service
public class TransactionMessageServiceImpl implements TransactionMessageService {

    @Autowired
    private TransactionMessageMapper mapper;

    @Override
    public boolean sendMessage(TransactionMessage message) {
        if (check(message)){
            int i = mapper.insert(message);
            if (i==1){
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public boolean sendMessage(List<TransactionMessage> messages) {
        for (TransactionMessage s:messages){
            if (!check(s)){
                return false;
            }

        }
        return false;
    }

    /**
     * 确认消息已被消费
     * @param consumerSystem
     * @param id
     * @return
     */
    @Override
    public boolean confirmCustomerMessage(String consumerSystem, Long id) {
        TransactionMessage message = mapper.selectById(id);
        if (message==null){
            return false;
        }
        message.setConsumerDate(new Date());    //设置消费时间
        message.setStatus(1);           //修改消费状态为已消费
        message.setConsumerSystem(consumerSystem);          //设置消费系统
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        int update = mapper.update(message,wrapper);
        if (update!=0){
            return true;
        }
        return false;
    }


      /** @Author GuiJie
       * @Description //TODO 查询等待消费的消息
       * @Date 9:57 2020/10/16
       * @Param
       * @return
       */
    @Override
    public List<TransactionMessage> findByWatingMessage() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status",0);
        return mapper.selectList(wrapper);
    }

    @Override
    public boolean confirmDieMessage(Long id) {
        TransactionMessage transactionMessage = mapper.selectById(id);
        if (transactionMessage==null){
            return false;
        }
        transactionMessage.setStatus(2);
        transactionMessage.setDieDate(new Date());
        int i = mapper.updateById(transactionMessage);
        if (i!=0){
            return true;
        }
        return false;
    }

    /**
     * 累计发送次数以及更新发送时间
     */
    @Override
    public boolean incrSendCount(Long id, Date parse) {
        TransactionMessage message = mapper.selectById(id);
        if (message==null){
            return false;
        }
        message.setSendDate(parse);
        message.setSendCount(message.getSendCount()+1);
        int i = mapper.updateById(message);
        if (i!=0){
            return true;
        }
        return false;
    }

    @Override
    public boolean retrySendDieMessage() {
        QueryWrapper wrapper = new QueryWrapper();

        return false;
    }

    @Override
    public List<TransactionMessage> findMessageByStatus(Integer status) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status",status);
        return mapper.selectList(wrapper);
    }


    private boolean check(TransactionMessage message) {
        if (!StringUtils.hasText(message.getMessage()) || !StringUtils.hasText(message.getQueue())
                || !StringUtils.hasText(message.getSendSystem())) {
            return false;
        }
        if (message.getcDate() == null) {
            return false;
        }
        return true;
    }
}
